import { Button, Modal, message, Spin } from 'antd';

const confirm = Modal.confirm;
import { WeaErrorPage, WeaTools, WeaAlertPage, WeaSearchGroup, WeaFormItem, WeaNewScroll } from 'ecCom';
import { WeaTableNew, WeaSwitch } from 'comsMobx';

const WeaTable = WeaTableNew.WeaTable;
import { inject, observer } from 'mobx-react';
import { toJS } from 'mobx';
import { WeaLocaleProvider } from 'ecCom';

const getLabel = WeaLocaleProvider.getLabel;
@observer
class OutResourceList extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
    };
    window._crmTable = {
      reLoad: props.contentStore.reLoad,
    };
  }
  componentDidMount() {
    	 const { contentStore, location: { query } } = this.props;
    contentStore.getOutResourceList(query);
    contentStore.getOutReourceRightMenu({ selectType: 'outresourceinfo', customerId: query.customerId });
  }
  componentWillUnmount() {
    const { contentStore } = this.props;
    contentStore.clearStatus();
  }
  render() {
    const { contentStore } = this.props;
    const { verified, hasRight, tableStore, showAddForm } = contentStore;
    if (verified && !hasRight) {
      return (<WeaAlertPage ecId={`${this && this.props && this.props.ecId || ''}_WeaAlertPage@ubpl5t`} >
        <div style={{ color: '#000' }}>
          {getLabel(2012, '对不起，您暂时没有权限！')}
        </div>
      </WeaAlertPage>
      );
    }
    if (verified && hasRight) {
      return (
        <WeaNewScroll ecId={`${this && this.props && this.props.ecId || ''}_WeaNewScroll@u6zrrb`} height={'100%'}>
          <div>

            {
              !showAddForm ?
                <WeaTable
                  ecId={`${this && this.props && this.props.ecId || ''}_WeaTable@dfpn4r`}
                  comsWeaTableStore={tableStore}
                  hasOrder
                  needScroll
                  onOperatesClick={this.onOperatesClick.bind(this)}
                /> :
                <div>
                  {this.getAddForm()}
                </div>
            }

          </div>
        </WeaNewScroll>
      );
    }
    return <div />;
  }
    getAddForm=() => {
      const { contentStore } = this.props;
      const { form, addFormCondition } = contentStore;
      const { isFormInit } = form;
      const group = [];
      isFormInit && toJS(addFormCondition).map((c,indexGroup) => {
        const items = [];
        c.items.map((fields, index) => {
          items.push({
            com: (<WeaFormItem
              ecId={`${this && this.props && this.props.ecId || ''}_WeaFormItem@6py7dy@${index}`}
              label={`${fields.label}`}
              labelCol={{ span: `${fields.labelcol}` }}
              error={form.getError(fields)}
              tipPosition="bottom"
              wrapperCol={{ span: `${fields.fieldcol}` }}
            >
              <WeaSwitch ecId={`${this && this.props && this.props.ecId || ''}_WeaSwitch@rhquiw@${index}`} fieldConfig={fields} form={form} />
            </WeaFormItem>),
            colSpan: 1,
          });
        });
        group.push(<WeaSearchGroup ecId={`${this && this.props && this.props.ecId || ''}_WeaSearchGroup@vaapdk@${indexGroup}`} height="100%" needTigger title={c.title} showGroup={c.defaultshow} items={items} />);
      });
      return group;
    }
    onOperatesClick(record, index, operate, flag) {
      const { contentStore, customerId } = this.props;
      // let _href = operate && operate.href ? operate.href : "";
      // let fn = _href.replace("javascript:","");
      // fn = fn.substring(0,fn.indexOf('('));
      if (flag == '0') { // 编辑
        openFullWindowHaveBarForCrm(`/main/crm/outinterfaceview?operate=edit&id=${record.randomFieldId}`);
      }
      if (flag == '1') { // 删除
        contentStore.deleteOutResource({ operation: 'delete', id: record.randomFieldId }, customerId);
      }
    }
}

export default WeaTools.tryCatch(React,
  props => <WeaErrorPage ecId={`${this && this.props && this.props.ecId || ''}_WeaErrorPage@cc863g`} msg={props.error ? props.error : getLabel(129417, '服务器运行出错，请联系系统管理员！')} />,
  { error: '' },
)(OutResourceList);
